package com.clubs.coding.Controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
//import com.clubs.coding.utils.SaticScheduleTask;
import com.clubs.coding.common.ApiRestResponse;
import com.clubs.coding.model.query.MesgList;
import com.clubs.coding.utils.SaticScheduleTask;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
@ResponseBody
@RequestMapping("/msg")

public class MessageController {
   @Autowired
SaticScheduleTask saticScheduleTask;


    private String appid = "wx9c3d61e786f8245d";
//    @Value("${wx.AppSecret}")
    private String AppSecret ="c105fc82fc62fb7fca80a95caa648e17";
    ArrayList<String> objectArrayList = new ArrayList<>();

    public String getAccessToken() {

        String result = cn.hutool.http.HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + AppSecret);
        System.out.println(result +"kankan zhel");
        JSONObject jsonObject = JSONUtil.parseObj(result);
        return jsonObject.getStr("access_token");
    }

    @RequestMapping("/send")
    public ApiRestResponse send(String openid){
        objectArrayList.add(openid);
        MesgList mesgList = new MesgList();
        mesgList.setOpenid(objectArrayList);
        saticScheduleTask.setMesgList(mesgList);
        return ApiRestResponse.success();
    }

public void go(String openid){

//    JSONObject body=new JSONObject();
//    body.set("touser",openid);
//    body.set("template_id","L8k5GsIVyiwaqC0EGCi0SGYpoyl76p9c0bhD3V7ccqg");
//    JSONObject json=new JSONObject();
//    json.set("phrase4",new JSONObject().set("value","当前报名人数低于三人不开课"));
//    json.set("thing5",new JSONObject().set("value", new Date()));
//    body.set("data",json);
//    //发送
//    String accessToken= getAccessToken();
//    String post =  cn.hutool.http.HttpUtil.post("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken, body.toString());
//    System.out.println("发起了" +post);
    RestTemplate restTemplate = new RestTemplate();
    //TODO 没有整合redis 每次获取最新的sendUrl
    String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken();
    //拼接推送的模版
    WxMssVo wxMssVo = new WxMssVo();
    wxMssVo.setTouser(openid);//用户的openid
    wxMssVo.setTemplate_id("2bDYbmltCFOOkTGKL-ZBngsgrWHAEndZaeg4QrTgdLQ");//订阅消息模板id
    wxMssVo.setPage("pages/index/index");
       Map<String, TemplateData> m = new HashMap<>();
        m.put("phrase4", new TemplateData("预约失败"));
    m.put("thing5", new TemplateData("预约失败请点开次消息查看"));
        wxMssVo.setData(m);

    wxMssVo.setData(m);



    ResponseEntity<String> responseEntity =
            restTemplate.postForEntity(url, wxMssVo, String.class);
System.out.println(responseEntity);

}

    /**
     * @author wh
     * @date 2022/07/11
     * @description 定义消息内容
     */
    @Data
    public class TemplateData {


        private String value;

        public TemplateData(String value) {
            this.value = value;
        }
    }
        @Data
        public class WxMssVo {

            private String touser;//用户openid

            private String template_id;//订阅消息模版id

            private String page = "pages/index/index";//默认跳到小程序首页

            private Map<String, TemplateData> data;//推送文字
        }


    }
